<?php

class Collaborateur {

    function __construct() {
        $db = mysql_connect("localhost", "root", "root");
        mysql_selectdb("projet2", $db);
    }

    function AfficherTaches() {

        $sql0 = mysql_query("select logincol from test where nom='logincol'");
        $row = mysql_fetch_array($sql0);
        $logincol = $row['logincol'];
        echo '<table border="1"><caption> LISTE DES TACHES</caption>  <th>nom tache</th> <th>pourcentage</th> <th>date cloture</th> <th>etat</th> <th>service</th> <th>date debut</th> <th>charge</th> <th>commentaire</th> <th>validation</th> <th>rejet</th> <th>terminer</th>';
        $sql1 = mysql_query("select distinct nomtache,pourcentage,datecloture,etat,service,datedebut,charge,commentaire from tache  where logincollaborateur='$logincol'");
        while ($row1 = mysql_fetch_array($sql1)) {
            $test = $row1['etat'];
            if ($test == "cloturé") {
                echo"<tr><td>".$row1['nomtache'] . "</td><td>" . $row1['pourcentage'] . "</td><td>" . $row1['datecloture'] . "</td><td>" . $row1['etat'] . "</td><td>" . $row1['service'] . "</td><td>" . $row1['datedebut'] . "</td><td>" . $row1['charge'] . "</td><td>" . $row1['commentaire'] . "</td><td> </td><td> </td><td> </td></tr>";
            }
            elseif($test == "en cours")
            {   $nomtache = $row1['nomtache']; 
                echo "<tr><td>".$row1['nomtache'] . "</td><td>" . $row1['pourcentage'] . "</td><td>" . $row1['datecloture'] . "</td><td>" . $row1['etat'] . "</td><td>" . $row1['service'] . "</td><td>" . $row1['datedebut'] . "</td><td>" . $row1['charge'] . "</td><td>" . $row1['commentaire'] . " </td><td> </td><td> <a href='rejetertache.php?nomtache=$nomtache'>rejeter</a>  </td><td> <a href='terminertache.php?nomtache=$nomtache'>terminer</a> </td></tr>"; 
            }
            else
            {  $nomtache = $row1['nomtache']; 
                echo "<tr><td>".$row1['nomtache'] . "</td><td>" . $row1['pourcentage'] . "</td><td>" . $row1['datecloture'] . "</td><td>" . $row1['etat'] . "</td><td>" . $row1['service'] . "</td><td>" . $row1['datedebut'] . "</td><td>" . $row1['charge'] . "</td><td>" . $row1['commentaire'] . " </td><td> <a href='validertache.php?nomtache=$nomtache'>valider</a> </td><td> <a href='rejetertache.php?nomtache=$nomtache'>rejeter</a>  </td><td>  <a href='terminertache.php?nomtache=$nomtache'>terminer</a> </td></tr>";
            }
            }
            echo '</table>';
        
    }

    public function ValiderTache() {

        if (isset($_POST['datedebuttache']) && isset($_POST['charge']) && isset($_POST['commentairevalidation']) && isset($_GET['nomtache'])) {
            $date = $_POST['datedebuttache'];
            $charge = $_POST['charge'];
            $commentaire = $_POST['commentairevalidation'];
            $nomtache = $_GET['nomtache'];
            $sql100 = mysql_query("select loginmanager from tache where nomtache='$nomtache'");  
            $data100 = mysql_fetch_array($sql100);
            $loginmanager = $data100['loginmanager'];
            $sql01 = mysql_query("select email from manager where login='$loginmanager'");
            $data1=  mysql_fetch_array($sql01);
            $mailmanager = $data1['email'];
            
            $sql22 = mysql_query("select logincol from test where nom='logincol'");
            $data22 = mysql_fetch_array($sql22);
            $logincol = $data22['logincol'];
            $sql23 = mysql_query("select email from collaborateur where login='$logincol'");
            $data23 = mysql_fetch_array($sql23);
            $mailcol = $data23['email'];
            
            ini_set("display_errors",0);error_reporting(0);
             ini_set("SMTP","smtp.topnet.tn");
            $message ="j'ai accepté votre tache".$nomtache."" ;
            
             $to = $mailmanager."";
            $subject = "acceptation de tache";

            $from = $mailcol."";
            $headers = "From:" . $from;
            if (mail($to, $subject, $message, $headers)) {
                echo "<script language='javascript'>alert('le manager a été informé'); </script>";
            } else {
                echo "<script language='javascript'>alert('mail non envoyé'); </script>";
            }
            
            
            $sql = mysql_query("update tache set datedebut='$date',charge='$charge',commentaire='$commentaire',etat='en cours' where nomtache='$nomtache'");

            if ($sql) {
                echo "<script language='javascript'>alert('validé avec success'); </script>";
            } else {
                echo "<script language='javascript'>alert('echec de validation'); </script>";
            }
        }
    }

    public function RejeterTache() {

        if (isset($_GET['nomtache']) && isset($_POST['commentairejet'])) {
            $nomtache = $_GET['nomtache'];
            $commentaire = $_POST['commentairejet'];

            $sql100 = mysql_query("select loginmanager from tache where nomtache='$nomtache'");  
            $data100 = mysql_fetch_array($sql100);
            $loginmanager = $data100['loginmanager'];
            $sql01 = mysql_query("select email from manager where login='$loginmanager'");
            $data1=  mysql_fetch_array($sql01);
            $mailmanager = $data1['email'];
            
            $sql22 = mysql_query("select logincol from test where nom='logincol'");
            $data22 = mysql_fetch_array($sql22);
            $logincol = $data22['logincol'];
            $sql23 = mysql_query("select email from collaborateur where login='$logincol'");
            $data23 = mysql_fetch_array($sql23);
            $mailcol = $data23['email'];
           
            ini_set("display_errors",0);error_reporting(0);
             ini_set("SMTP","smtp.topnet.tn");
            $message ="j'ai décliné la tache | indication :".$commentaire."" ;
            
             $to = $mailmanager."";
            $subject = "déclinaison tache";

            $from = $mailcol."";
            $headers = "From:" . $from;
            if (mail($to, $subject, $message, $headers)) {
                echo "<script language='javascript'>alert('le manager a été informé'); </script>";
            } else {
                echo "<script language='javascript'>alert('mail non envoyé'); </script>";
            }
            
            
            $sql = mysql_query("update tache set etat='rejeté' where nomtache='$nomtache'");
            if ($sql) {
                echo "<script language='javascript'>alert('la tache a été  rejeté  avec les explications fournit'); </script>";
            } 
            else {
                echo "<script language='javascript'>alert('echec'); </script>";
            }
        }
    }
    
    public function TerminerTache() {
        
        if (isset($_GET['nomtache']))
        {
            $nomtache = $_GET['nomtache'];
            $sql100 = mysql_query("select loginmanager from tache where nomtache='$nomtache'");  
            $data100 = mysql_fetch_array($sql100);
            $loginmanager = $data100['loginmanager'];
            $sql01 = mysql_query("select email from manager where login='$loginmanager'");
            $data1=  mysql_fetch_array($sql01);
            $mailmanager = $data1['email'];
            
            $sql22 = mysql_query("select logincol from test where nom='logincol'");
            $data22 = mysql_fetch_array($sql22);
            $logincol = $data22['logincol'];
            $sql23 = mysql_query("select email from collaborateur where login='$logincol'");
            $data23 = mysql_fetch_array($sql23);
            $mailcol = $data23['email'];
            
            ini_set("display_errors",0);error_reporting(0);
             ini_set("SMTP","smtp.topnet.tn");
            $message ="j'ai terminé la tache".$nomtache."" ;
            
             $to = $mailmanager."";
            $subject = "tache terminé";

            $from = $mailcol."";
            $headers = "From:" . $from;
            if (mail($to, $subject, $message, $headers)) {
                echo "<script language='javascript'>alert('le manager a été informé '); </script>";
            } else {
                echo "<script language='javascript'>alert('mail non envoyé'); </script>";
            }
            
             $sql = mysql_query("update tache set etat='terminé' where nomtache='$nomtache'");
            if ($sql) {
                echo "<script language='javascript'>alert('la  tache est terminé'); </script>";
            } 
            else {
                echo "<script language='javascript'>alert('echec'); </script>";
            }
                       
        }
    }

}

?>
